package org.springblade.modules.dingtalk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.mapper.NoticeMapper;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.dingtalk.entity.DeptSyncOU;
import org.springblade.modules.dingtalk.mapper.DeptSyncOUMapper;
import org.springblade.modules.dingtalk.service.IDeptSyncOUService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptSyncOUServiceImpl extends BaseServiceImpl<DeptSyncOUMapper, DeptSyncOU> implements IDeptSyncOUService {


	@Override
	public List<DeptSyncOU> getDeptTreeList(List<DeptSyncOU> resultDept) {
		List<DeptSyncOU> list = this.list(new QueryWrapper<DeptSyncOU>().eq("parent_id",0));
		for (DeptSyncOU deptSyncOU : list) {
			DeptSyncOU tree = getTree(deptSyncOU);
			if (tree != null) {
				resultDept.add(tree);
			}
		}
		return resultDept;

	}



	public DeptSyncOU getTree(DeptSyncOU deptSyncOU){
		List<DeptSyncOU> child = this.list(new QueryWrapper<DeptSyncOU>().eq("parent_id", deptSyncOU.getDeptId()));
		if (child!=null && child.size()>0) {
			deptSyncOU.setChildren(child);
			for (DeptSyncOU son : child) {
				if (son.getParentId()!=0) {
					getTree(son);
				}
			}
		}
		return deptSyncOU;
	}



	@Override
	public List<DeptSyncOU> listDept() {
		List<DeptSyncOU> list = this.list();
		return list;
	}
}
